﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bzure.Apps.Security;
using Bzure.Common.Data;
using Bzure.Security.Principal;
using Bzure.Common.Core;

namespace System
{
    public static class UserGroupExtensions
    {
        public static UserGroup GetUserGroup(this IUserGroupContract UserGroupContract, string GroupID)
        {
            UserGroup factor = new UserGroup()
            {
                ID = GroupID
            };
            EntityQueryRequest<UserGroup> query = new EntityQueryRequest<UserGroup>();
            query.AppendConditionMap(factor.ID, () => factor.ID);
            return Actor.Public.BuildEntityProcessor("SecurityApp").QueryEntity(query).FirstOrDefault();
        }
        /// <summary>
        /// 我创建的用户组
        /// </summary>
        /// <param name="Me"></param>
        /// <param name="BlockName"></param>
        /// <returns></returns>
        public static UserGroup[] MyUserGroups(this Me Me, string BlockName = null)
        {
            UserGroup factor = new UserGroup()
            {
                OwnerName = Me.Name
            };
            EntityQueryRequest<UserGroup> query = new EntityQueryRequest<UserGroup>();
            query.AppendConditionMap(factor.OwnerName, () => factor.OwnerName);
            if (BlockName.IsNotNull())
            {
                factor.BlockName = BlockName;
                query.AppendConditionMap(factor.BlockName, () => factor.BlockName);
            }
            return Actor.Public.BuildEntityProcessor("SecurityApp").QueryEntity(query);
        }
        public static UserGroup NewUserGroup(this Me Me, string BlockName, string GroupName, string Description, int State = 0)
        {
            UserGroup group = new UserGroup()
            {
                ID = Guid.NewGuid().ToString("N"),
                BlockName = BlockName,
                Name = GroupName,
                OwnerName = Me.Name,
                Description = Description,
                State = State
            };
            group.UpdateEntity();
            return group;
        }
        public static UserGroupMap[] GetGroupMapsForUserJoined(this IUserGroupContract UserGroupContract, string UserID)
        {
            UserGroupMap factor = new UserGroupMap()
            {
                UserID = UserID
            };
            EntityQueryRequest<UserGroupMap> query = new EntityQueryRequest<UserGroupMap>();
            query.AppendConditionMap(factor.UserID, () => factor.UserID);
            return Actor.Public.BuildEntityProcessor("SecurityApp").QueryEntity(query);
        }

        public static UserGroup[] GetGroupsForUserJoined(this IUserGroupContract UserGroupContract, string UserID, string BlockName = null)
        {
            string[] GroupIDs = UserGroupContract.GetGroupMapsForUserJoined(UserID).Select(m => m.GroupID).ToArray();
            UserGroup[] ugs = UserGroupContract.GetUserGroups(GroupIDs);
            if (BlockName.IsNotNull())
            {
                ugs = ugs.Where(m => m.BlockName == BlockName).ToArray();
            }
            return ugs;
        }
        public static BzureUser[] GetMembers(this UserGroup UserGroup)
        {
            UserGroupMap factor = new UserGroupMap()
            {
                GroupID = UserGroup.ID
            };
            EntityQueryRequest<UserGroupMap> query = new EntityQueryRequest<UserGroupMap>();
            query.AppendConditionMap(factor.GroupID, () => factor.GroupID);
            string[] Ids = Actor.Public.BuildEntityProcessor("SecurityApp").QueryEntity(query).Select(m => m.UserID).ToArray();
            return Actor.Public.AsUserManager().GetUsers(Ids);
        }
        public static void UpdateEntity(this UserGroup UserGroup)
        {
            UserGroup.UpdateEntity("SecurityApp");
        }
        public static void DeleteEntity(this UserGroup UserGroup)
        {
            UserGroup.DeleteEntity("SecurityApp");
        }
        public static void UpdateEntity(this UserGroupMap UserGroupMap)
        {
            UserGroupMap.UpdateEntity("SecurityApp");
        }
        public static void DeleteEntity(this UserGroupMap UserGroupMap)
        {
            UserGroupMap.DeleteEntity("SecurityApp");
        }
        public static void RemoveUserGroupMap(this IUserGroupContract UserGroupContract, string MapID)
        {
            UserGroupMap factor = new UserGroupMap()
            {
                ID = MapID
            };
            EntityPostRequest request = new EntityPostRequest(EntityPostRequest.EntityPostMethod.Delete)
            {
                Entity = factor
            };
            request.AppendConditionMap(factor.ID, () => factor.ID);
            Actor.Public.BuildEntityProcessor("SecurityApp").UpdateEntity(request);
        }
        public static void RemoveUserGroupMap(this IUserGroupContract UserGroupContract, string GroupID, string UserID)
        {
            UserGroupMap factor = new UserGroupMap()
            {
                GroupID = GroupID,
                UserID = UserID
            };
            EntityPostRequest request = new EntityPostRequest(EntityPostRequest.EntityPostMethod.Delete)
            {
                Entity = factor
            };
            request.AppendConditionMap(factor.GroupID, () => factor.GroupID);
            request.AppendConditionMap(factor.UserID, () => factor.UserID);
            Actor.Public.BuildEntityProcessor("SecurityApp").UpdateEntity(request);
        }
    }
}
